﻿using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;
using Relocation.Data;

namespace Relocation.Data
{

	public static class ModelFunctionExpend
	{


		#region Project
		/// <summary>
		/// 是否被使用
		/// </summary>
		public static bool IsUsed(this Project source)
		{	
			//安置户
			if (source.Relocatees.CreateSourceQuery().Any())
				return true;
			//村庄
			if (source.Villages.CreateSourceQuery().Any())
				return true;
			//项目文件
			if (source.Files.CreateSourceQuery().Any())
				return true;
			return false;
		}

		public static string GetNodeText(this Project source)
		{
			return source.name;
		}

		public static string GetNodeToolTipText(this Project source)
		{
			return string.Format("征地单位： {0}| 项目创建日：{1}", source.organization, source.created.HasValue?source.created.Value.ToString("yyyy年MM月dd日"):string.Empty);
		}

		public static void LoadFiles(this Project source)
		{
			if (!source.Files.IsLoaded)
				source.Files.Load();
		}

		public static string GetImageKey(this Project source)
		{
			return "Project";
		}
		
		#endregion

		#region House
		/// <summary>
		/// 是否被使用
		/// </summary>
		public static bool IsUsed(this House source)
		{
			return source.Relocatees_House.CreateSourceQuery().Any();
		}

		public static void LoadRelocateHouse(this House source)
		{
			if (!source.Relocatees_House.IsLoaded)
				source.Relocatees_House.Load();
		}

		public static EntityCollection<Relocatees_House> GetRelocateHouse(this House source)
		{
			source.LoadRelocateHouse();
			return source.Relocatees_House;
		}

		#endregion

		#region User
		public static bool IsGeneral(this User source)
		{
			return "general".Equals(source.role);
		} 
		#endregion

		#region Placement

		public static void LoadRelocate(this Placement source)
		{
			if (!source.RelocateeReference.IsLoaded)
				source.RelocateeReference.Load();
		}

		public static string GetNodeText(this Placement source)
		{
			return source.name + (string.IsNullOrEmpty(source.id_number) ? "" : ("(" + source.id_number + ")"));
		}

		public static string GetNodeToolTipText(this Placement source)
		{
			return string.Format("与户主关系：{0}", source.relation);
		}

		public static string GetImageKey(this Placement source)
		{
			return "Placement";
		}

		public static bool IsHead(this Placement source)
		{
			return "户主".Equals(source.relation);
		}

		/// <summary>
		/// 返回拆迁户信息（自动加载）
		/// </summary>
		/// <param name="source"></param>
		public static Relocatee GetRelocatee(this Placement source)
		{
			if (!source.RelocateeReference.IsLoaded)
				source.RelocateeReference.Load();
			return source.Relocatee;
		}

		#endregion

		#region Relocatees_House

		public static string GetNodeText(this Relocatees_House source)
		{
			return source.House.district + " " + source.House.house_code + "(" + source.House.storeroom + ")";
		}

		public static string GetNodeToolTipText(this Relocatees_House source)
		{
			StringBuilder sb = new StringBuilder();
			sb.Append(string.Format("安置房面积：{0}", source.House.area1));
			sb.Append(string.Format(" | 储藏室面积：{0}", source.House.area2));
			sb.Append(string.Format(" | 安置房款：{0}", source.money1));
			sb.Append(string.Format(" | 储藏室款：{0}", source.money2));
			sb.Append("\n");
			sb.Append(string.Format("其他费用：{0}", source.money3 ));
			sb.Append(string.Format(" | 货币化安置款冲抵后实收：{0}", source.total2));
			sb.Append(string.Format(" | 水电开户费：{0}", source.money4));
			sb.Append(string.Format(" | 装修押金：{0}", source.money5));
			return sb.ToString();			
		}

		public static string GetImageKey(this Relocatees_House source)
		{
			return "House";
		}

		public static void LoadRelocate(this Relocatees_House source)
		{
			if (!source.RelocateeReference.IsLoaded)
				source.RelocateeReference.Load();
		}

		/// <summary>
		/// 返回拆迁户信息（自动加载）
		/// </summary>		
		public static Relocatee GetRelocate(this Relocatees_House source)
		{
			source.LoadRelocate();
			return source.Relocatee;
		}
		#endregion

		#region Relocatee
		public static void LoadPlacments(this Relocatee soucre)
		{
			if (!soucre.Placements.IsLoaded)
				soucre.Placements.Load();
		}

		public static void LoadRelocateHouse(this Relocatee soucre)
		{
			if (!soucre.Relocatees_House.IsLoaded)
				soucre.Relocatees_House.Load();
		}

		public static void LoadProject(this Relocatee soucre)
		{
			if (!soucre.ProjectReference.IsLoaded)
				soucre.ProjectReference.Load();
		}

		public static string GetNodeText(this Relocatee source)
		{
			string headIdNumber = "";
			//string headIdNumber = source.Placements.Where(t => t.relation.Equals("户主")).FirstOrDefault().id_number;
			return source.head + (string.IsNullOrEmpty(headIdNumber) ? string.Empty : ("（" + headIdNumber + "）"));
		}

		public static string GetNodeToolTipText(this Relocatee source)
		{
			StringBuilder sb = new StringBuilder();
			sb.Append(string.Format("拆迁补偿款：{0}", source.money1));
			sb.Append(string.Format(" | 房屋统拆补助：{0}", source.money2));
			sb.Append(string.Format(" | 拆迁奖励：{0}", source.money3));
			sb.Append("\n");
			sb.Append(string.Format("货币化安置款：{0}", source.payment));
			sb.Append(" | 意向安置户型：");
			sb.Append(string.Format("{0} (50㎡) ", source.housetype5));
			sb.Append(string.Format("{0} (70㎡) ", source.housetype1));
			sb.Append(string.Format("{0} (90㎡) ", source.housetype2));
			sb.Append(string.Format("{0} (110㎡) ",source.housetype3 ));
			sb.Append(string.Format("{0} (130㎡) ", source.housetype4));
			sb.Append(string.Format("{0} (储藏室) ", source.housetype6));
			sb.Append(string.Format("{0} (门市) ", source.housetype7));
			return sb.ToString();
			
		}

		public static string GetImageKey(this Relocatee source)
		{
			return (source.status == 1) ? "Relocatees1" : (source.status == 2) ? "Relocatees2" : "Relocatees";			
		}

		#region Organization
		public static string GetNodeTextOrganization(this Relocatee source)
		{
			return source.organization;
		}

		public static string GetNodeToolTipTextOrganization(this Relocatee source)
		{
			return source.organization;
		}

		public static string GetImageKeyOrganization(this Relocatee source)
		{
			return "Organization";
		} 
		#endregion

		#region Village
		public static string GetNodeTextVillage(this Relocatee source)
		{
			return source.village;
		}

		public static string GetNodeToolTipTextVillage(this Relocatee source)
		{
			return source.village;
		}

		public static string GetImageKeyVillage(this Relocatee source)
		{
			return "Village";
		} 
		#endregion

				

		
		#endregion




		
	}
}
